# -*- coding: utf-8 -*-
import logging
import re

from logging.handlers import TimedRotatingFileHandler


def singleton(cls):
    instance = {}

    def _singleton(*args, **kw):
        if cls not in instance:
            instance[cls] = cls(*args, **kw)
        return instance[cls]
    return _singleton


@singleton
class logger(object):
    """docstring for ClassName"""

    def __init__(self):
        self.log_file_handler = TimedRotatingFileHandler(filename="error", when="D", interval=1, backupCount=90)
        self.suffix = "%Y-%m-%d_%H-%M.log"
        self.log_file_handler.extMatch = re.compile(r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}.log$")
        self.log_file_handler.setFormatter(logging.Formatter('%(asctime)s\tFile \"%(filename)s\",line %(lineno)s\t%(levelname)s: %(message)s'))
        logging.basicConfig(level=logging.INFO)
        self.logger = logging.getLogger()
        self.logger.addHandler(self.log_file_handler)

    def debug(self, data):
        self.logger.debug(data)

    def info(self, data):
        self.logger.info(data)

    def warn(self, data):
        self.logger.warning(data)

    def error(self, data):
        self.logger.error(data)

    def critical(self, data):
        self.logger.critical(data)

    def exception(self, data):
        self.logger.exception(data)

    def set_log_level(self, level):
        self.log_level = level
        self.logger.setLevel(self.log_level)

    def exit(self):
        self.logger.removeHandler(self.log_file_handler)
